package resman.database;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * Classe que gerencia a base de dados
 */
public class DatabaseHandler
{

	private static SessionFactory sessionFactory;
	private static ServiceRegistry serviceRegistry;
	
	/**
	 * Inicializa a base de dados
	 */
	static
	{
		Configuration configuration = new Configuration().configure();
	    serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
	    		.buildServiceRegistry();
	    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	}

	/**
	 * Inicia uma sessão da base de dados
	 * @param isTransaction Indicador de transação
	 * @return Sessão da base de dados
	 */
	public static TransSession openSession(boolean isTransaction)
	{
		return new TransSession(sessionFactory.openSession(), isTransaction);
	}

}
